<%-- 


    Document   : role
    Created on : 2016-7-1, 14:13:07
    Author     : 52461
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@include file="../link.jsp"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body style="font-family: 微软雅黑;padding-top: 10px;" ng-app="roleManager" ng-controller="mainCtrl">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading"><span style="float: left">角色列表</span>
                    <div>
                        <input ng-model="searchContent" placeholder="名字/病人ID" style="width:320px; float: left;margin-left: 20px" class="form-control input-sm" />
                        <button ng-click="getList()" style="float: left" class="btn btn-success btn-sm"><span class="fa fa-search"></span>&nbsp;&nbsp;搜索</button>

                        <button ng-click="showCreate()" style="margin-left: 30px;float: left" class="btn btn-info btn-sm"><span class="fa fa-send"></span>&nbsp;&nbsp;添加</button></div>

                    <div style="clear: both"></div>
                </div>
                <table class="table">
                    <tr>
                        <th>编号</th>
                        <th>名称</th>
                        <th>描述</th>
                        <th>时间</th>
                        <th>操作</th>


                    </tr>
                    <tr ng-repeat="list in lists">
                        <!--                        <td>
                                                    <input ng-model="list.send" type="checkbox"></td>-->
                        <td>{{$index}}</td>
                        <td>{{list.name}}</td>
                        <td>{{list.description}}</td>
                        <td>{{list.createtime}}</td>

                        <td>
                            <button ng-click="editRole(list)" ng-click="editRole(list)" class="btn btn-success btn-xs"><span class="fa fa-edit"></span>&nbsp;修改</button>
                            <button ng-click="deleteRole(list)" class="btn btn-danger btn-xs"><span class="fa fa-bolt"></span>&nbsp;删除</button>
                        </td>
                    </tr>

                </table>

                <footer class="panel-heading" >
                    <div class="row">
<!--                        <div class="col-sm-4 hidden-xs">

                        </div>
                        <div class="col-sm-4 text-center">
                                                        <small class="text-muted inline m-t-sm m-b-sm">showing 20-30 of 50 items</small>
                        </div>-->
                        <div  style="text-align: center">                
                            <pagination ng-change="pageChanged()" previous-text="上一页" next-text="下一页" first-text="第一页" last-text="最后一页" ng-model="data.currentPage" items-per-page="data.pageSize" total-items="data.bigTotalItems" ng-model="data.bigCurrentPage" max-size="data.maxSize" class="pagination-sm" boundary-links="true" rotate="false" num-pages="numPages"></pagination>

                        </div>
                    </div>
                </footer>

            </div>
            <!--            <div style="width: 100%;text-align: center">
                            <pagination ng-change="pageChanged()" previous-text="上一页" next-text="下一页" first-text="第一页" last-text="最后一页" ng-model="data.currentPage" items-per-page="data.pageSize" total-items="data.bigTotalItems" ng-model="data.bigCurrentPage" max-size="data.maxSize" class="pagination-sm" boundary-links="true" rotate="false" num-pages="numPages"></pagination>
            
                        </div>-->
        </div>


        <div class="modal fade mode-x" role="dialog" aria-labelledby="gridSystemModalLabel">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="gridSystemModalLabel">新角色</h4>
                    </div>
                    <div class="modal-body">
                        <form  name="users" id="users" onsubmit="return false;">
                            <table style="width: 100%" class="cx-table">
                                <tr>
                                    <td>名称</td>
                                    <td>
                                        <input ng-model="user.name" required="" type="text" class="form-control">
                                    </td>

                                </tr>
                                <tr>
                                    <td>描述</td>
                                    <td>
                                        <input ng-model="user.description" required="" type="text" class="form-control">
                                    </td>

                                </tr>
                                <tr>
                                    <td colspan="2">
                                        <div ng-repeat="auth in auths" class="col-lg-4">
                                            <div class="col-lg-3"><input ng-model="auth.select" type="checkbox"/></div>
                                            <div class="col-lg-9">{{auth.name}}</div>
                                        </div>
                                    </td>
                                </tr>

                            </table>
                        </form>

                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <button  ng-click="saveRole()" ng-disabled="users.$invalid" type="button" class="btn btn-primary"><span class="fa fa-save"></span>&nbsp;&nbsp;保存</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->

    </body>
</html>
<script>
    angular.module("roleManager", ['baseModule', 'ui.bootstrap'])

            .controller("mainCtrl", function ($scope, base) {

                var so = $scope;
                $scope.data = {currentPage: 1};
                so.setPage = function (pageNo) {
                    $scope.data.currentPage = pageNo;
                };
                so.pageChanged = function () {
                    so.getList();
                };
                so.data.maxSize = 10;
                so.data.bigTotalItems = 20;
                so.data.bigCurrentPage = 1;
                so.data.pageSize = 10;
                $scope.user = {};
                $scope.showCreate = function () {
                    $scope.user = {};
                    $(".mode-x").modal('show');
                }
                $scope.getList = function () {
                    base.post("role/getList", {pageSize: so.data.pageSize, currentPage: so.data.currentPage}, function (data) {

                        if (data) {
                            so.lists = data.list;
                            so.data.bigTotalItems = data.totalRow;
                        }

                    })
                };
                $scope.getRole = function () {
                    base.post("role/getAuth").get(function (data) {

                        $scope.auths = data;

                        $scope.baseAuths = angular.copy(data);

                    });
                }
                $scope.editRole = function (item) {
                    $scope.auths = angular.copy($scope.baseAuths);

                    $scope.user = item;
                    $(".mode-x").modal('show');
                    base.post("role/getRoleAuth", {pk: item.pk}).get(function (data) {
                        if (data) {
                            for (i in data) {
                                for (n in $scope.auths) {
                                    if ($scope.auths[n].pk == data[i].res_id) {
                                        $scope.auths[n].select = true;
                                    }
                                }
                            }
                        }
                    })
                }
                $scope.deleteRole = function (item) {
                    steveConfrim("确定删除吗?", function () {
                        base.post("role/deleteRole", {pk: item.pk}).get(function (data) {
                            if (data == "true") {
                                steveAl("删除成功", 1000);
                                $scope.getList();
                            } else {
                                steveAl("删除失败", 1000);
                            }
                        })
                    })
                }
                $scope.saveRole = function () {
                    var selectAuth = [];
                    for (i in $scope.auths) {
                        if ($scope.auths[i].select) {
                            selectAuth.push($scope.auths[i].pk);
                        }
                    }
                    $scope.user.auth = selectAuth.join(",");
//                    console.log($scope.user);
//                    return;
                    if ($scope.user) {
                        base.post("role/saveRole", $scope.user).get(function (data) {
                            if (data == 'true') {
                                steveAl("保存成功", 1000);
                                $(".mode-x").modal('hide');
                                $scope.getList();
                            } else {
                                steveAl("保存失败", 1000);
                            }
                        });
                    }
                }
                var init = function () {
                    $scope.getList();
                    $scope.getRole();
                }
                init();
            })
</script>
